package com.imedia.frame.lucene.service;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jeasy.analysis.MMAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.ChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.imedia.frame.lucene.utils.IndexUtils;

/**
 * <p>Title: 全站检索</p>
 * <p>Description: lucene检索模块抽象类</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: </p>
 * @author qiuy
 * @version 1.0
 */
public abstract class AbstractSearchService implements ISearchService{
	public  static final String INDEX_PATH = IndexUtils.getMessage("lucene.index.path"); //索引目录
	protected static Analyzer standardAnalyzer; //lucene默认分词器 也支持中文
	protected static Analyzer chineseAnalyzer;  //中文分词器
	protected static Analyzer mmAnalyzer;       //mm分词器 相对好用点
	private static Directory directory;
	private static File indexFile = new File(INDEX_PATH);
	protected QueryParser queryParser;
	protected IndexSearcher searcher;
	protected Query query;
	
	static{
		standardAnalyzer = new StandardAnalyzer();
		
		chineseAnalyzer = new ChineseAnalyzer();
		
		mmAnalyzer = new MMAnalyzer();
		try {
			
			directory = FSDirectory.getDirectory(indexFile);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * <p>Description: 初始化queryParser 用StandardAnalyzer分词器</p>
	 * @param isNew
	 */
	protected void initStandardAnalyzerIndexSearcher(){
		try {
			
			this.searcher = new IndexSearcher(directory);
			queryParser = new QueryParser("content",standardAnalyzer);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * <p>Description: 初始化queryParser 用MMAnalyzer分词器</p>
	 * @param isNew
	 */
	protected void initMMAnalyzerIndexSearcher(){
		try {
			
			this.searcher = new IndexSearcher(directory);
			queryParser = new QueryParser("content",mmAnalyzer);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * <p>Description: 初始化queryParser 用ChineseAnalyzer分词器</p>
	 * @param isNew
	 */
	protected void initChineseAnalyzerIndexSearcher(){
		try {
			
			this.searcher = new IndexSearcher(directory);
			queryParser = new QueryParser("content",chineseAnalyzer);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * <p>Description: 初始化searcher</p>
	 */
	protected abstract void initSearcher();
	@SuppressWarnings("unchecked")
	public List search(String keywords) {
		// TODO Auto-generated method stub
		return null;
	}
	@SuppressWarnings("unchecked")
	public List searchByContent(Object object) {
		// TODO Auto-generated method stub
		return null;
	}
	@SuppressWarnings("unchecked")
	public List searchByTitle(Object object) {
		// TODO Auto-generated method stub
		return null;
	}
}
